package com.lifo.user.controller;

import com.fasterxml.jackson.core.JsonProcessingException;
import com.lifo.good.bean.model.HfGoods;
import com.lifo.good.service.SearchGoodsService;
import com.lifo.user.bean.model.HfGoodsFavorites;
import com.lifo.user.service.HfGoodsFavoritesService;
import com.lifo.utils.common.date.DateUtil;
import com.lifo.utils.Page;
import com.lifo.utils.wrapper.ResponseMsgBean;
import com.lifo.utils.wrapper.WrapperJson;
import lombok.NonNull;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.util.StringUtils;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;

import javax.servlet.http.HttpSession;

/**
 * Created by Administrator on 2018/6/26.
 */
@Slf4j
@Controller
public class HfGoodsFavoritesController {

    @Autowired
    private HfGoodsFavoritesService hfGoodsFavoritesService;

    @Autowired
    private SearchGoodsService searchGoodsService;


    @Autowired
    private HttpSession session;

    @RequestMapping("/user/favoriteGoods")
    public String getUserFavoriteGoods(Model model) {

       /* page = page > 0 ?  1 : page;*/

        //查询记录数
        Integer endIndex = 50;
        //起始索引
        Integer startIndex = 0;

        //获取userId
        Integer userId = (int) session.getAttribute("userId");

        Page<HfGoodsFavorites> favoritesGoods = hfGoodsFavoritesService.searchUserFavoritesGoods(userId, startIndex, endIndex);

        model.addAttribute("favoritesGoods", favoritesGoods.getRows());

        return "/center/favorites";
    }

    /**
     * 保存收藏
     *
     * @param hfGoodsFavorites
     * @return
     */
    @RequestMapping("/user/saveFavoriteGoods")
    public
    @ResponseBody
    String saveUserFavoriteGoods(HfGoodsFavorites hfGoodsFavorites) throws JsonProcessingException {

        Integer userId = (int) session.getAttribute("userId");
        //封装参数
        hfGoodsFavorites.setUserId(userId);
        hfGoodsFavorites.setCreateTime(DateUtil.getNowDateString());
        hfGoodsFavorites.setCreateDate(DateUtil.getNowDateString("yyyy-MM-dd"));
        HfGoods hfGoods = searchGoodsService.searchHfGoodsByHfUid(Integer.parseInt(String.valueOf(hfGoodsFavorites.getHfGoodId())));
        hfGoodsFavorites.setSmallPicture(hfGoods.getSmallPicture().split(";")[0]);
        //查询商品是否存在
        HfGoodsFavorites hfGoodsFavoritesResult = hfGoodsFavoritesService.searchFavoritesGoodByHfGoodId(hfGoodsFavorites.getHfGoodId(), userId);
        log.debug("商品已在收藏夹中存在==>{}",new WrapperJson().toJson(hfGoodsFavoritesResult));
        if (StringUtils.isEmpty(hfGoodsFavoritesResult)) {
            log.debug("即将保存商品到收藏夹,商品信息为==>{}",new WrapperJson().toJson(hfGoodsFavorites));
            Integer result = hfGoodsFavoritesService.saveUserFavoritesGoods(hfGoodsFavorites);
            log.debug("保存商品到收藏夹成功,商品编号==>{}",hfGoodsFavorites.getHfGoodId());
            if (result > 0) {
                return ResponseMsgBean.getMsgJsonWithSuccess();
            }
        }else{
            return ResponseMsgBean.getMsgJsonWithSuccess();
        }
        return ResponseMsgBean.getMsgJsonWithSystemError();
    }

    /**
     * 删除收藏
     *
     * @param hfGoodId
     * @return
     */
    @RequestMapping("/user/delFavoriteGoods")
    public
    @ResponseBody
    String delUserFavoriteGoods(@NonNull long hfGoodId) throws JsonProcessingException {
        Integer userId = (int) session.getAttribute("userId");

        HfGoodsFavorites hfGoodsFavorites = new HfGoodsFavorites();

        hfGoodsFavorites.setUserId(userId);
        hfGoodsFavorites.setHfGoodId(Integer.parseInt(String.valueOf(hfGoodId)));

        Boolean result = hfGoodsFavoritesService.delUserFavoritesGoods(hfGoodsFavorites);

        if (result) {
            return ResponseMsgBean.getMsgJsonWithSuccess();
        }
        return ResponseMsgBean.getMsgJsonWithSystemError();
    }
}
